package com.shop2cn.iapi.member.testcase;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.shop2cn.iapi.member.parameter.CreateMemberBean;
import com.shop2cn.iapi.member.service.CreateMemberCall;
import com.ymatou.iapi.dguser.parameter.UserCreateBean;
import com.ymt.core.tool.FormatUtil;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.DGUserCallService;
import com.ymttest.business.service.GodMemberService;
import com.ymttest.business.service.shenqi.GodmchManageCallService;
import com.ymttest.database.model.godmember.Member;
import com.ymttest.database.sqlwapper.godmember.GodMemberWapper;
import com.ymttest.utils.TestDataManager;
/**
*创建会员
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_CreateMember{
	private static CreateMemberBean creatememberBean;
	private static CreateMemberCall creatememberCall;
	private static Integer shopid ;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("创建会员");
		try {
			shopid= GodmchManageCallService.CreateSellerMch();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Before
	public void caseUp() {
		creatememberBean=new CreateMemberBean();
		creatememberCall=new CreateMemberCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(creatememberCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	private void verify(CreateMemberBean creatememberBean2, String memberId) throws InterruptedException {
		GodMemberWapper GodMemberWapper=new GodMemberWapper();
		Member mem = GodMemberWapper.selectMemberByMemberid(memberId).get(0);
		Logger.debug("mem:"+FormatUtil.beanToGSONString(mem));
		Logger.verifyEquals(creatememberBean2.getAccessSource(), mem.getAccessSource(), "验证AccessSource");
		Logger.verifyEquals(creatememberBean2.getOpenId(), mem.getOpenId(), "验证openid");
		Logger.verifyEquals(creatememberBean2.getShopId(), mem.getShopId(), "验证shopid");
		if(creatememberBean2.getUserId()!=null&&creatememberBean2.getUserId()!=0) {
			Logger.verifyEquals(creatememberBean2.getUserId(), mem.getUserId(), "验证userid");
		}else {
			Logger.verifyIsNull(mem.getUserId(), "验证userid");
		}
		Logger.verifyNotNull(mem.getUpdateTime(), "验证UpdateTime");
		Logger.verifyNotNull(mem.getCreateTime(), "验证CreateTime");
		Logger.verifyNotNull(mem.getAccessTime(), "验证AccessTime");
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CreateMember_001() {
		Logger.start(true,"创建会员成功");
		try {
			shopid= GodmchManageCallService.CreateSellerMch();
			UserCreateBean usercreateBean = DGUserCallService.initUsercreateBean();
			int userId = DGUserCallService.UserCreateCall(usercreateBean);
			
			creatememberBean.setAccessSource(1);
			creatememberBean.setOpenId(usercreateBean.getOpenId());
			creatememberBean.setShopId(shopid);
			creatememberBean.setUserId(userId);
			creatememberCall.setData(creatememberBean);
			creatememberCall.callService();			
			Logger.verifyEquals("200", creatememberCall.getString("code"), "验证code");
			Logger.verifyEquals(16, creatememberCall.getCreateMemberResp().getMemberId().length(), "会员号长度");
			verify(creatememberBean,creatememberCall.getCreateMemberResp().getMemberId());
			//20200602
			Logger.verifyEquals(false, creatememberCall.getCreateMemberResp().getNeedInvitationCode(), "验证是否是会员邀请制度");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	

	@Test
	@Category({P2.class})
	@TestCase
	@Ignore
	//不输入userid的case转换成匿名用户case
	public void Tc_CreateMember_002() {
		Logger.start(true,"不输入userid失败");
		try {
			creatememberBean.setAccessSource(1);
			creatememberBean.setOpenId("openid");
			creatememberBean.setShopId(shopid);
//			creatememberBean.setUserId(1);
			creatememberCall.setData(creatememberBean);
			creatememberCall.callService();			
			Logger.verifyEquals("400", creatememberCall.getString("code"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CreateMember_003() {
		Logger.start(true,"不输入shopid失败");
		try {
			creatememberBean.setAccessSource(1);
			creatememberBean.setOpenId("openid");
//			creatememberBean.setShopId(shopid);
			creatememberBean.setUserId(1);
			creatememberCall.setData(creatememberBean);
			creatememberCall.callService();			
			Logger.verifyEquals("400", creatememberCall.getString("code"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CreateMember_004() {
		Logger.start(true,"userid为0不输入openid失败");
		try {
			creatememberBean.setAccessSource(1);
//			creatememberBean.setOpenId(TestDataManager.getRandomUUID().toString());
			creatememberBean.setShopId(shopid);
			creatememberBean.setUserId(0);
			creatememberCall.setData(creatememberBean);
			creatememberCall.callService();			
			Logger.verifyEquals("400", creatememberCall.getString("code"), "验证code");
//			verify(creatememberBean,creatememberCall.getCreateMemberResp().getMemberId());
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CreateMember_005() {
		Logger.start(true,"userid不为0不输入openid-openid不能为空");
		try {
			UserCreateBean usercreateBean = DGUserCallService.initUsercreateBean();
			int userId = DGUserCallService.UserCreateCall(usercreateBean);
			creatememberBean.setAccessSource(1);
//			creatememberBean.setOpenId(usercreateBean.getOpenId());
			creatememberBean.setShopId(shopid);
			creatememberBean.setUserId(userId);
			creatememberCall.setData(creatememberBean);
			creatememberCall.callService();			
			Logger.verifyEquals("300", creatememberCall.getString("code"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CreateMember_006() {
		Logger.start(true,"不输入accessSource失败");
		try {
//			creatememberBean.setAccessSource(1);
			creatememberBean.setOpenId(TestDataManager.getRandomUUID().toString());
			creatememberBean.setShopId(shopid);
			creatememberBean.setUserId(0);
			creatememberCall.setData(creatememberBean);
			creatememberCall.callService();			
			Logger.verifyEquals("400", creatememberCall.getString("code"), "验证code");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CreateMember_007() {
		Logger.start(true,"匿名用户openid已存在失败");
		try {
			creatememberBean.setAccessSource(1);
			creatememberBean.setOpenId(TestDataManager.getRandomUUID().toString());
			creatememberBean.setShopId(shopid);
//			creatememberBean.setUserId(0);
			creatememberCall.setData(creatememberBean);
			creatememberCall.callService();			
			Logger.verifyEquals("200", creatememberCall.getString("code"), "验证code");
			String mid = creatememberCall.getCreateMemberResp().getMemberId();
			verify(creatememberBean,mid);
			creatememberCall.callService();			
			Logger.verifyEquals("200", creatememberCall.getString("code"), "验证code");
			//openid重复则不会创建新用户还是返回第一次创建
			Logger.verifyEquals(mid, creatememberCall.getCreateMemberResp().getMemberId(), "不会创建新用户");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CreateMember_008() {
		Logger.start(true,"shop中userid重复失败");
		try {
			UserCreateBean usercreateBean = DGUserCallService.initUsercreateBean();
			int userId = DGUserCallService.UserCreateCall(usercreateBean);
			String openid1=usercreateBean.getOpenId();
			creatememberBean.setAccessSource(1);
			creatememberBean.setOpenId(openid1);
			creatememberBean.setShopId(shopid);
			creatememberBean.setUserId(userId);
			creatememberCall.setData(creatememberBean);
			creatememberCall.callService();			
			Logger.verifyEquals("200", creatememberCall.getString("code"), "验证code");
			verify(creatememberBean,creatememberCall.getCreateMemberResp().getMemberId());
			String openid2="oid" + TestDataManager.getRandomNum(6) + System.currentTimeMillis() % 1000;
			creatememberBean.setOpenId(openid2);
			creatememberCall.callService();			
			Logger.verifyEquals("200", creatememberCall.getString("code"), "验证code");
			//以userid为准 返回创建成功但是openid不变
			creatememberBean.setOpenId(openid1);
			verify(creatememberBean,creatememberCall.getCreateMemberResp().getMemberId());
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CreateMember_009() {
		Logger.start(true,"在不同shop中创建同一个匿名openid相同成功");
		try {
			creatememberBean.setAccessSource(1);
			creatememberBean.setOpenId(TestDataManager.getRandomUUID().toString());
			creatememberBean.setShopId(shopid);
			creatememberBean.setUserId(0);
			creatememberCall.setData(creatememberBean);
			creatememberCall.callService();			
			Logger.verifyEquals("200", creatememberCall.getString("code"), "验证code");
			verify(creatememberBean,creatememberCall.getCreateMemberResp().getMemberId());
			Integer shopid2 = GodmchManageCallService.CreateSellerMch();
			creatememberBean.setShopId(shopid2);
			creatememberCall.callService();			
			Logger.verifyEquals("200", creatememberCall.getString("code"), "验证code");
			verify(creatememberBean,creatememberCall.getCreateMemberResp().getMemberId());
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CreateMember_010() {
		Logger.start(true,"在不同shop中创建同一个userid成功");
		try {
			UserCreateBean usercreateBean = DGUserCallService.initUsercreateBean();
			int userId = DGUserCallService.UserCreateCall(usercreateBean);
			creatememberBean.setAccessSource(1);
			creatememberBean.setOpenId(TestDataManager.getRandomUUID().toString());
			creatememberBean.setShopId(shopid);
			creatememberBean.setUserId(userId);
			creatememberCall.setData(creatememberBean);
			creatememberCall.callService();			
			Logger.verifyEquals("200", creatememberCall.getString("code"), "验证code");
			verify(creatememberBean,creatememberCall.getCreateMemberResp().getMemberId());
			Integer shopid2 = GodmchManageCallService.CreateSellerMch();
			creatememberBean.setShopId(shopid2);
			creatememberCall.callService();			
			Logger.verifyEquals("200", creatememberCall.getString("code"), "验证code");
			verify(creatememberBean,creatememberCall.getCreateMemberResp().getMemberId());
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CreateMember_011() {
		Logger.start(true,"创建匿名用户成功");
		try {
			creatememberBean.setAccessSource(1);
			creatememberBean.setOpenId(TestDataManager.getRandomUUID().toString());
			creatememberBean.setShopId(shopid);
			creatememberBean.setUserId(0);
			creatememberCall.setData(creatememberBean);
			creatememberCall.callService();			
			Logger.verifyEquals("200", creatememberCall.getString("code"), "验证code");
			verify(creatememberBean,creatememberCall.getCreateMemberResp().getMemberId());
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	public void Tc_CreateMember_012() {
		Logger.start(true,"重复创建匿名用户成功");
		try {
			creatememberBean.setAccessSource(1);
			creatememberBean.setOpenId(TestDataManager.getRandomUUID().toString());
			creatememberBean.setShopId(shopid);
//			creatememberBean.setUserId(0);
			creatememberCall.setData(creatememberBean);
			creatememberCall.callService();			
			Logger.verifyEquals("200", creatememberCall.getString("code"), "验证code");
			verify(creatememberBean,creatememberCall.getCreateMemberResp().getMemberId());
			creatememberBean.setOpenId(TestDataManager.getRandomUUID().toString());
			creatememberCall.callService();			
			Logger.verifyEquals("200", creatememberCall.getString("code"), "验证code");
			verify(creatememberBean,creatememberCall.getCreateMemberResp().getMemberId());
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P2.class})
	@TestCase
	@Ignore
	//业务测试灌数据用
	public void Tc_CreateMember_013() {
		Logger.start(true,"测试数据");
		try {
			for(int i=0;i<30;i++) {
			UserCreateBean usercreateBean = DGUserCallService.initUsercreateBean();
			int userId = DGUserCallService.UserCreateCall(usercreateBean);
			creatememberBean.setAccessSource(1);
			creatememberBean.setOpenId(usercreateBean.getOpenId());
			creatememberBean.setUserId(userId);
			creatememberBean.setShopId(500747523);
			creatememberCall.setData(creatememberBean);
			creatememberCall.callService();			
			Logger.verifyEquals("200", creatememberCall.getString("code"), "验证code");
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CreateMember_014() {
		Logger.start(true,"创建会员成功-会员邀请");
		try {
			shopid= GodmchManageCallService.CreateSellerMch();
			GodMemberService.setCollectionMobile(shopid, true);
			GodMemberWapper GodMemberWapper=new GodMemberWapper();
			GodMemberWapper.updateMchmemberConfigNeedInvitationCode(shopid, true);
			UserCreateBean usercreateBean = DGUserCallService.initUsercreateBean();
			int userId = DGUserCallService.UserCreateCall(usercreateBean);
			creatememberBean.setAccessSource(1);
			creatememberBean.setOpenId(usercreateBean.getOpenId());
			creatememberBean.setShopId(shopid);
			creatememberBean.setUserId(userId);
			creatememberCall.setData(creatememberBean);
			creatememberCall.callService();			
			Logger.verifyEquals("200", creatememberCall.getString("code"), "验证code");
			Logger.verifyEquals(16, creatememberCall.getCreateMemberResp().getMemberId().length(), "会员号长度");
			verify(creatememberBean,creatememberCall.getCreateMemberResp().getMemberId());
			//20200602
			Logger.verifyEquals(true, creatememberCall.getCreateMemberResp().getNeedInvitationCode(), "验证是否是会员邀请制度");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CreateMember_015() {
		Logger.start(true,"创建手机号商户相同渠道不同的会员成功");
		try {
			shopid= GodmchManageCallService.CreateSellerMch();
			UserCreateBean usercreateBean = DGUserCallService.initUsercreateBean();
			int userId = DGUserCallService.UserCreateCall(usercreateBean);
			
			UserCreateBean usercreateBean2 = DGUserCallService.initUsercreateBean();
			int userId2 = DGUserCallService.UserCreateCall(usercreateBean2);
			
			UserCreateBean usercreateBean3 = DGUserCallService.initUsercreateBean();
			int userId3 = DGUserCallService.UserCreateCall(usercreateBean3);
			
			creatememberBean.setAccessSource(1);
			creatememberBean.setOpenId(usercreateBean.getOpenId());
			creatememberBean.setShopId(shopid);
			creatememberBean.setUserId(userId);
			creatememberCall.setData(creatememberBean);
			creatememberCall.callService();			
			Logger.verifyEquals("200", creatememberCall.getString("code"), "验证code");
			String m1=creatememberCall.getCreateMemberResp().getMemberId();
			Logger.verifyEquals(16, creatememberCall.getCreateMemberResp().getMemberId().length(), "会员号长度");
			verify(creatememberBean,creatememberCall.getCreateMemberResp().getMemberId());
			//20200602
			Logger.verifyEquals(false, creatememberCall.getCreateMemberResp().getNeedInvitationCode(), "验证是否是会员邀请制度");
			
			creatememberBean.setOpenId(usercreateBean2.getOpenId());
			creatememberBean.setUserId(userId2);
			creatememberBean.setAccessSource(4);
			creatememberCall.callService();			
			Logger.verifyEquals("200", creatememberCall.getString("code"), "验证code");
			verify(creatememberBean,creatememberCall.getCreateMemberResp().getMemberId());
			String m2=creatememberCall.getCreateMemberResp().getMemberId();
			Logger.verifyNotEquals(m1, m2, "验证新创建会员");
			
			creatememberBean.setOpenId(usercreateBean3.getOpenId());
			creatememberBean.setUserId(userId3);
			creatememberBean.setAccessSource(5);
			creatememberCall.callService();			
			Logger.verifyEquals("200", creatememberCall.getString("code"), "验证code");
			verify(creatememberBean,creatememberCall.getCreateMemberResp().getMemberId());
			String m3=creatememberCall.getCreateMemberResp().getMemberId();
			Logger.verifyNotEquals(m2, m3, "验证新创建会员");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}